import * as THREE from "three";
import { Config } from "./Config";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
import { Tooler } from "./tooler";
import { Cache } from "./Cache";
import { Panel } from "./Panel";
export class Card extends THREE.Object3D {
  mesh: THREE.Mesh = null as any;
  colorMesh: THREE.Mesh = null as any;
  img: THREE.Mesh = null as any;
  panel: Panel;

  constructor() {
    super();
    this.panel = new Panel();
    this.add(this.panel)
  }


  init(mesh: THREE.Mesh) {
    this.mesh = mesh;
    // this.colorMesh = new THREE.Mesh(mesh.geometry.clone(), new THREE.MeshPhongMaterial({color: 0x19811e}));

    this.add(this.mesh);
    // this.add(this.colorMesh);

    // console.log("old", this.mesh.scale)
    // console.log("new", this.colorMesh.scale)

    // this.mesh.visible = false;
    // this.colorMesh.scale.copy(this.mesh.scale)
    // this.colorMesh.visible = true;

    // this.run(false);
  }

  run() {
    this.panel.play()
  }

  setType(type: any) {
    if (!this.img) {
      this.img = new THREE.Mesh(new THREE.BoxGeometry(Config.CARD_WIDTH, Config.CARD_HEIGHT, Config.CARD_DEPTH), new THREE.MeshStandardMaterial({ alphaTest: 0.2, transparent: true, color: 0xffffff }))
      this.add(this.img);
      this.img.scale.set(0.98, 0.98, 1);
      this.img.position.z = 1;

      // const box = new THREE.Mesh(new THREE.BoxGeometry(Config.CARD_WIDTH, Config.CARD_HEIGHT, Config.CARD_DEPTH), new THREE.MeshStandardMaterial({ alphaTest: 0.2, transparent: true, color: 0xffffff, opacity: 0.72 }))
      // this.add(box);
      // box.scale.set(0.8, 0.8, 0.8);
      // box.position.z = Config.CARD_DEPTH * 0.2;
    }
    (this.img.material as any).map = Cache.CARD_MAT_MAP[type];
  }

  static load() {
    return new Promise<void>(resolve => {
      Cache.PANEL_GEO = new THREE.BoxGeometry(Config.CARD_WIDTH, Config.CARD_HEIGHT, Config.CARD_DEPTH)
      resolve();
    });
  }

  static oldLoad() {
    return new Promise<void>(resolve => {
      // 加载 GLTF 模型
      const loader = new GLTFLoader();
      loader.load(
        './3d/cube/scene.gltf', // 替换为您的 GLTF 模型路径
        (gltf) => {
          const model = gltf.scene;
          console.log("model cube", model)

          model.traverse((child) => {
            if (child.name == "Cube_0") {
              // 在这里对每个 Mesh 进行操作
              console.log(child);
              // 计算模型的包围盒
              const box = new THREE.Box3().setFromObject(child);
              const size = box.getSize(new THREE.Vector3());

              console.log("size", size, "box", box)

              // 计算缩放因子
              const maxDim = Math.max(size.x, size.y, size.z);
              const scale = Config.CARD_WIDTH / maxDim;

              console.log("scale", scale, child.position)

              // 应用缩放
              child.scale.set(Config.CARD_WIDTH / size.z, Config.CARD_HEIGHT / size.y, Config.CARD_DEPTH / size.z);
              // child.material.opacity = 0.5;
              (child as any).material.color = new THREE.Color(0x0f888f)

              Cache.PANEL_GEO = (child as THREE.Mesh).geometry


              // child.material.transparent = true;
              // child.rotation.x = 90 * (Math.PI / 180);
              // const card = new Card();
              // card.init(child as any);
              // Config.card = card;
              resolve();

              // const textureLoader = new THREE.TextureLoader();
              // const texture = textureLoader.load(`/pic/handmah_30.png`);
              // const card = new THREE.Mesh(new THREE.BoxGeometry(Config.CARD_WIDTH, Config.CARD_HEIGHT, Config.CARD_DEPTH), new THREE.MeshBasicMaterial({ map: texture }))

              // const group = new THREE.Object3D();
              // group.add(child);

              // group.add(card);
              // card.scale.set(0.8, 0.8, 0.8);
              // card.position.z = Config.CARD_DEPTH * 0.2
              // group.position.z = 60;
              // resolve(group);
            }
          });

        }
      );
    })

  }
}